using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using SeataNet.Core.Database.MySQL;
using SeataNet.Core.Database.PostgreSQL;
using SeataNet.Core.Database.SqlServer;

namespace SeataNet.Core.Database
{
    /// <summary>
    /// 数据库服务扩展
    /// </summary>
    public static class DatabaseServiceExtensions
    {
        /// <summary>
        /// 添加MySQL数据库支持
        /// </summary>
        public static IServiceCollection AddMySqlDatabase(this IServiceCollection services)
        {
            services.TryAddSingleton<IDatabaseDialect, MySqlDialect>();
            services.TryAddSingleton<ITableMetaCache, DefaultTableMetaCache>();
            return services;
        }

        /// <summary>
        /// 添加PostgreSQL数据库支持
        /// </summary>
        public static IServiceCollection AddPostgreSqlDatabase(this IServiceCollection services)
        {
            services.TryAddSingleton<IDatabaseDialect, PostgreSqlDialect>();
            services.TryAddSingleton<ITableMetaCache, DefaultTableMetaCache>();
            return services;
        }

        /// <summary>
        /// 添加SQL Server数据库支持
        /// </summary>
        public static IServiceCollection AddSqlServerDatabase(this IServiceCollection services)
        {
            services.TryAddSingleton<IDatabaseDialect, SqlServerDialect>();
            services.TryAddSingleton<ITableMetaCache, DefaultTableMetaCache>();
            return services;
        }

        /// <summary>
        /// 添加自定义数据库方言
        /// </summary>
        public static IServiceCollection AddDatabaseDialect<TDialect>(this IServiceCollection services)
            where TDialect : class, IDatabaseDialect
        {
            services.TryAddSingleton<IDatabaseDialect, TDialect>();
            services.TryAddSingleton<ITableMetaCache, DefaultTableMetaCache>();
            return services;
        }
    }
}
